<?php
class CdeReferencelinksController extends AppController {
	var $name = 'CdeReferencelinks';
	var $helpers = array('Html', 'Form');

	// Add new reference link for module - DONE
	function add($moduleId = null) {
		// Check if the reference link ID is provided, if not display error message and redirect to index of all units
		if (!$moduleId && empty($this->data)) {
			$this->Session->setFlash(__('Reference link ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
		
		// Get module details by module ID
		$this->set('cdeModule', $this->CdeReferencelink->CdeModule->read(null, $moduleId));

		if (!empty($this->data)) {
			$this->CdeReferencelink->create();
			if ($this->CdeReferencelink->save($this->data)) {
				// Add new reference link successfully, redirect back to manage page of the module
				$this->Session->setFlash(__('The new reference link has been successfully added', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'../cde_Modules/manage/'.$this->data['CdeReferencelink']['cde_module_id']));
			} else {
				// Error occurred, cannot add new reference link
				$this->Session->setFlash(__('An error occurred, new reference link could not be successfully added. Please try again!', true), 'default', array('class' => 'error-message'));
			}
		}
	}
	
	// Update reference link - DONE
	function edit($id = null) {
		// Check if the reference link ID is provided, if not display error message and redirect to index of all units
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Reference link ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}

		// Get the reference link by ID
		$cdeReferencelink = $this->CdeReferencelink->read(null, $id);
		if (!$cdeReferencelink && empty($this->data)) {
			// If the reference link is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid reference link ID, cannot edit', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		} else {
			$this->set('cdeReferencelink', $cdeReferencelink);
			if (!empty($this->data)) {
				// On update action
				if ($this->CdeReferencelink->save($this->data)) {
					// Update successfully, redirect back to manage page of the module
					$this->Session->setFlash(__('The reference link has been successfully updated', true), 'default', array('class' => 'success-message'));
					$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeReferencelink['CdeReferencelink']['cde_module_id']));
				} else {
					// Error occurred, cannot update
					$this->Session->setFlash(__('The reference link could not be updated. Please try again!', true), 'default', array('class' => 'error-message'));
				}
			} else {
				// Set the data to update
				$this->data = $cdeReferencelink;
			}
		}
	}

	// Delete reference link - DONE
	function delete($id = null) {
		// Check if the reference link ID is provided, if not display error message and redirect to index of all units
		if (!$id) {
			$this->Session->setFlash(__('Reference link ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}

		// Get the reference link by ID
		$cdeReferencelink = $this->CdeReferencelink->read(null, $id);
		if ($cdeReferencelink != null) {
			// If the reference link is found, delete it
			if ($this->CdeReferencelink->del($id)) {
				// Delete successfully, redirect back to manage page of the module
				$this->Session->setFlash(__('The reference link has been successfully deleted', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeReferencelink['CdeReferencelink']['cde_module_id']));
			} else {
				// Error occurred, cannot delete, redirect back to manage page of the module
				$this->Session->setFlash(__('An error occurred, cannot delete the reference link. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'../cde_modules/manage/'.$cdeReferencelink['CdeReferencelink']['cde_module_id']));
			}
		} else {
			// If the reference link is not found display the error message and redirect to index of all units
			$this->Session->setFlash(__('Invalid reference link ID, cannot delete', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'../cde_units/index'));
		}
	}
}
?>